package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.ejb.Util.DataUtils;
import br.com.tiespec.model.Enum.SituClientePortal;
import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.ClientePortal;

@Stateless(mappedName = "ClientePortalDAO")
public class ClientePortalDAO extends DAO {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	public List<ClientePortal> listarPendenteAprovacao(int first, int pageSize,
			String sortField, TipoSortOrder sortOrder, String varNome,
			String varUsuario, Date varCadastroDe, Date varCadastroAte) {
		List<ClientePortal> cpList = new ArrayList<ClientePortal>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(ClientePortal.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("situacao", SituClientePortal.PENDENTE_APROVACAO));
			
			if (varCadastroDe != null)
				criteria.add(Restrictions.ge("dataCadastro", DataUtils.getEmptyTime(varCadastroDe)));
			if (varCadastroAte != null)
				criteria.add(Restrictions.le("dataCadastro", DataUtils.getFullTime(varCadastroAte)));
			if (isNullOrBlank(varNome))
				criteria.add(Restrictions.or(
						Restrictions.ilike("nomeCompleto", "%" + varNome + "%"),
						Restrictions.ilike("nomeCurto", "%" + varNome + "%")
						));
			if (isNullOrBlank(varUsuario)){
				criteria.createCriteria("responsavel")
					.add(Restrictions.or(
							Restrictions.ilike("nomeCompleto", "%" + varUsuario + "%"),
							Restrictions.ilike("nomeCurto", "%" + varUsuario + "%")
						));
			}
			
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			
			cpList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpList;
	}

	public Integer countPendenteAprovacao(String varNome, String varUsuario, Date varCadastroDe,
			Date varCadastroAte) {
		Integer count = 0;
		try {
			begin();
			
			Criteria criteria = this.session.createCriteria(ClientePortal.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("situacao", SituClientePortal.PENDENTE_APROVACAO));
			
			if (varCadastroDe != null)
				criteria.add(Restrictions.ge("dataCadastro", DataUtils.getEmptyTime(varCadastroDe)));
			if (varCadastroAte != null)
				criteria.add(Restrictions.le("dataCadastro", DataUtils.getFullTime(varCadastroAte)));
			if (isNullOrBlank(varNome))
				criteria.add(Restrictions.or(
						Restrictions.ilike("nomeCompleto", "%" + varNome + "%"),
						Restrictions.ilike("nomeCurto", "%" + varNome + "%")
						));
			if (isNullOrBlank(varNome)){
				criteria.createCriteria("responsavel")
					.add(Restrictions.or(
							Restrictions.ilike("nomeCompleto", "%" + varUsuario + "%"),
							Restrictions.ilike("nomeCurto", "%" + varUsuario + "%")
						));
			}
			
			criteria.setProjection(Projections.rowCount());
			count = Integer.parseInt(criteria.uniqueResult().toString());
			
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	public ClientePortal selecionar(Long id) {
		ClientePortal cpInstance = new ClientePortal();
		try {
			begin();
			cpInstance = (ClientePortal) this.session.createCriteria(ClientePortal.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpInstance;
	}
	
	public ClientePortal selecionarComResponsavel(Long id) {
		ClientePortal cpInstance = new ClientePortal();
		try {
			begin();
			cpInstance = (ClientePortal) this.session.createCriteria(ClientePortal.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("responsavel", FetchMode.JOIN).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpInstance;
	}

	public ClientePortal selecionarComUsuarios(Long id) {
		ClientePortal cpInstance = new ClientePortal();
		try {
			begin();
			cpInstance = (ClientePortal) this.session.createCriteria(ClientePortal.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("usuarios", FetchMode.JOIN).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpInstance;
	}

	@SuppressWarnings("unchecked")
	public List<ClientePortal> listarClientePortal(int first, int pageSize,
			String sortField, TipoSortOrder sortOrder, String varNome) {
		List<ClientePortal> cpList = new ArrayList<ClientePortal>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(ClientePortal.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("situacao", SituClientePortal.ATIVO));
			
			if (isNullOrBlank(varNome))
				criteria.add(Restrictions.or(
						Restrictions.ilike("nomeCompleto", "%" + varNome + "%"),
						Restrictions.ilike("nomeCurto", "%" + varNome + "%")
						));
			
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			
			cpList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpList;
	}

	public Integer countClientePortal(String varNome) {
		Integer count = 0;
		try {
			begin();
			
			Criteria criteria = this.session.createCriteria(ClientePortal.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("situacao", SituClientePortal.ATIVO));
					
			
			if (isNullOrBlank(varNome))
				criteria.add(Restrictions.or(
						Restrictions.ilike("nomeCompleto", "%" + varNome + "%"),
						Restrictions.ilike("nomeCurto", "%" + varNome + "%")
						));

			criteria.setProjection(Projections.rowCount());
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}
}